import { Component, OnInit } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
import { RollService} from "../../../../../my-service/roll.service";
import { NzMessageService } from 'ng-zorro-antd';
import {UtilService} from "../../../../../my-service/util.service";

@Component({
  selector: 'app-roll-class',
  templateUrl: './roll-class.component.html',
  styleUrls: ['./roll-class.component.css'],
})
export class RollClassComponent implements OnInit {
  RollClass={
    rollGuid:null,//班级Guid,
    width:null,//当前浏览器的宽度
    height:null,//当前网页的高度
    boxWidth:214,//每个盒子的宽度,算上右1px的border
    boxHeight:214,//每个盒子的高度，算上下1px的border
    rowTotal:null,//每一行能够摆多少个
    paddingWidth:300,//左右边距所占的宽度
    lastEmptyBox:null,//最后需要摆多少个空盒子
    totalBox:null,//总共有多少个盒子
    minList:null,//算上最少要几列铺满一个页面
    minBox:null,//计算最少需要多少个盒子，如果totalBox<minbox;不用计算最后一排需要放多少个盒子
    containerWidth:null,//计算装每个盒子容器的宽度为多少
    totalBoxArr:[],//装学生
    lastEmptyBoxArr:[],
    isMinute:false,//查看成绩详情
    currentIndex:0,//当前学生索引
    className:'',//当前学生名字
    haveHeight:277,//已经有的高度，分别是导航条，子导航，上下边距,距离标题的距离
    defaultImg:'/assets/img/rollIndex/touxiang.png',//用户默认头像
    isLoad:false,
    onCloseMinute:function (flage) {
      this.isMinute = flage;
    },
    getRowTotal:function () {
      this.rowTotal = Math.floor((this.width-this.paddingWidth)/this.boxWidth);
      this.containerWidth = this.rowTotal * this.boxWidth+1;
    },//计算一行能够摆多少个
    getLastEmptyBox:function () {
      this.lastEmptyBox = this.rowTotal-(this.totalBox%this.rowTotal);
    },//计算最后一列需要补放多少个空盒子
    getMinList:function () {
      this.totalBox=this.totalBoxArr.length  ;
      this.minList = Math.floor((this.height-this.haveHeight)/this.boxHeight);
      //计算需要补多少列
      this.minBox = this.minList*this.rowTotal;
      //计算需要多少个盒子
      if(this.minBox>=this.totalBox){
        this.lastEmptyBox = this.minBox-this.totalBox;
      }else {
        this.getLastEmptyBox();
      }
      //进行逻辑判断需要补多少个盒子
      /*以下为模拟数据用*/
      this.lastEmptyBoxArr.length = this.lastEmptyBox;
    },
    _constructor:function () {
      if(this.width<=1370&&this.width>1220){
        this.paddingWidth = 150;
      }else if (this.width<1220) {
        this.boxWidth = 180;
      }
      this.getRowTotal();
      this.getMinList();
    }//初始数据函数
  };
  constructor(
    private router:Router,
    private routerInfo:ActivatedRoute,
    private rollServe:RollService,
    private msg :NzMessageService,
    private utilServe:UtilService
  ) { }
  getStudentList(classGuid){
    this.rollServe.getStudentlist(classGuid).then(res=>{
      if(res.status == 1){
        this.RollClass.totalBoxArr = res.data;
        this.RollClass._constructor();
        this.RollClass.isLoad = true;
      }else {
        console.log(res);
        this.msg.error('服务器内部错误,请稍后再试');
      }
    })
  };//获取学生列表
   getClassInfo(classGuid) {
     this.rollServe.getClassInfo(classGuid).then(res=>{
       if(res.status == 1) {
         this.RollClass.className = res.data[0].ClassName;
       }else {
         console.log(res);
         this.msg.error('服务器内部错误,请稍后再试');
       }
     })
   }
   //获取班级信息
  ngOnInit() {
    this.RollClass.width=document.body.clientWidth;
    this.RollClass.height = document.body.clientHeight;
    this.RollClass.rollGuid = this.routerInfo.snapshot.params['id'];
    this.getStudentList(this.RollClass.rollGuid);
    this.getClassInfo(this.RollClass.rollGuid);
    let _this= this;
    let _document = document;
    //复制上下文环境
    window.onresize=function () {
      _this.RollClass.width= _document.body.clientWidth;
      _this.RollClass.height = _document.body.clientHeight;
      _this.RollClass._constructor();
    }//当视图窗口大小发生改变时，重新计算

  }

}
